package com.hncgy.xzf.jdapi.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hncgy.xzf.jdapi.domain.product.dto.SeckillProductDTO;
import com.hncgy.xzf.jdapi.domain.product.po.Products;
import com.hncgy.xzf.jdapi.domain.product.vo.ProductDetailsVO;
import com.hncgy.xzf.jdapi.domain.product.vo.ProductSeckillVO;
import com.hncgy.xzf.jdapi.service.product.IProductsService;
import com.hncgy.xzf.jdapi.tool.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 不会代码的蜡笔
 * @since 2025-10-31
 */
@RestController
@RequestMapping("/product/products")
public class ProductsController {

    @Autowired
    private IProductsService productsService;

    /**
     * 新增商品
     * @param products 商品信息
     * @return 操作结果
     */
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody Products products) {
        try {
            boolean result = productsService.save(products);
            return result ? Result.success(true) : Result.error("新增失败");
        } catch (Exception e) {
            return Result.error("新增失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询商品列表
     * @param page 页码
     * @param size 每页大小
     * @param products 查询条件
     * @return 商品列表
     */
    @GetMapping("/list")
    public Result<Page<Products>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Products products) {
        try {
            Page<Products> pageObj = new Page<>(page, size);
            QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .like(StringUtils.hasText(products.getProductName()),
                            Products::getProductName,
                            products.getProductName());
            Page<Products> result = productsService.page(pageObj, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询所有商品（不分页）
     * @param products 查询条件
     * @return 商品列表
     */
    @GetMapping("/listAll")
    public Result<List<Products>> listAll(Products products) {
        try {
            QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .like(StringUtils.hasText(products.getProductName()),
                            Products::getProductName,
                            products.getProductName());

            List<Products> result = productsService.list(queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取商品详情
     * @param productId 商品ID
     * @return 商品信息
     */
    @GetMapping("/{productId}")
    public Result<ProductDetailsVO> getByproductId(@PathVariable Integer productId) {
        try {
            ProductDetailsVO product = productsService.selectProductDetailsById(productId);
            if (product != null) {
                return Result.success(product);
            } else {
                return Result.error("商品不存在");
            }
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取秒杀商品详情
     * @param seckillProductDTO ID
     * @return 秒杀商品信息
     */
    @PostMapping("/seckillDetailId")
    public Result<ProductSeckillVO> getByproductId(@RequestBody SeckillProductDTO seckillProductDTO) {

        System.err.println(seckillProductDTO);
        try {
            ProductSeckillVO product = productsService.selectSeckillDetailsById(seckillProductDTO.getProductId() , seckillProductDTO.getPromotionId());
            if (product != null) {
                return Result.success(product);
            } else {
                return Result.error("秒杀商品不存在");
            }
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 更新商品信息
     * @param products 商品信息
     * @return 操作结果
     */
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody Products products) {
        try {
            boolean result = productsService.updateById(products);
            return result ? Result.success(true) : Result.error("更新失败");
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除商品
     * @param id 商品ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Integer id) {
        try {
            boolean result = productsService.removeById(id);
            return result ? Result.success(true) : Result.error("删除失败");
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
}
